import { createRouter, createWebHistory } from 'vue-router'
import HomeView from '../views/HomeView.vue'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      name: 'home',
      component: HomeView
    },
    {
      path: '/login',
      name: 'login',
      component: () => import('../views/login.vue')
    },
    {
      path: '/User',
      name: 'User',
      component: () => import('../views/User.vue')
    },
    {
      path: '/:catchAll(.*)',
      name: '404',
      component: () => import('../views/404.vue')
    }
  ]
})
router.beforeEach((to, from, next) => {
  let token = localStorage.getItem('data')
  if (!token && to.path != '/login') {
    next('/login')
  } else {
    next()
  }
})
export default router
